package com.tbit.uqbike.service.base.impl;

import com.tbit.uqbike.object.pojo.Account;
import com.tbit.uqbike.object.pojo.SuperPswConfig;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.service.base.SuperPswService;
import com.tbit.uqbike.webmanager.dao.core.AccountDao;
import com.tbit.uqbike.webmanager.dao.core.SuperPswConfigDao;
import com.tbit.uqbike.webmanager.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author MyWin E-mail:335918956@qq.com
 * @version 1.0
 * @createTime 2019-12-27 16:43
 */
@Service
public class SuperPswServiceImpl implements SuperPswService {
    @Autowired
    private SuperPswConfigDao superPswConfigDao;
    @Autowired
    private AccountDao accountDao;

    /**
     * 计算默认超级密码
     *
     * @param phone
     * @return
     */
    private static String calDefSuperPsw(String phone) {
        return MD5Util.MD5Encode(String.format("[%s]", phone), "utf-8");
    }

    @Override
    public boolean superPswCheck(String path, LoginInfo li, String superPsw) {
        List<SuperPswConfig> spcList = superPswConfigDao.selectByPath(path);
        for (SuperPswConfig item : spcList) {
            if (Objects.equals(superPsw, item.getSuperPwd())) {
                return true;
            }
        }
        if (null != li && li.haveLogin()) {
            Account account = accountDao.getByAccountId(li.id);
            if (null != account && account.getPhone() != null) {
                if (Objects.equals(superPsw, calDefSuperPsw(account.getPhone()))) {
                    return true;
                }
            }
        }
        return false;
    }
}
